package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.dao.EmployeeDao;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;
    @Override
    public Employee login(String username, String password) {
        //1.创建查询条件
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper();
        lqw.eq(Employee::getUsername,username).eq(Employee::getPassword,password);
        Employee employee = employeeDao.selectOne(lqw);
        return employee;
    }

    @Override
    public boolean save(Employee employee) {
       return employeeDao.insert(employee) > 0;
    }

    @Override
    public Page<Employee> findByPageAndConditions(int page, int pageSize, String name) {
        Page<Employee> p = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper();
        lqw.like(name != null,Employee::getName,name);
        employeeDao.selectPage(p, lqw);
        return p;
    }

    @Override
    public boolean update(Employee employee) {
       return employeeDao.updateById(employee) > 0;
    }

    @Override
    public Employee getById(Long id) {
        Employee employee = employeeDao.selectById(id);
        return employee;
    }

}
